<?php

/**
 * @class coupon
 * @brief 优惠券模块
 */
class Coupon
{
    public $error;

    public function __construct()
    {
        $this->logObj = new Log();

    }

    /**
     * 给用户发送优惠券
     *
     * @param        $coupon_group_id   优惠券主id
     * @param        $send_type         优惠发放类型
     * @param        $user_id           用户id
     * @param int    $num
     * @param        string             优惠券当前商品售卖的时候 自动发货领取的订单号
     * @param        string             通过道具发放 发放领取说明
     *
     * @return bool
     */
    public function setCouponToUser($coupon_group_id, $send_type, $user_id, $num = 1, $receive_order_no = '', $props_source = '')
    {
        if (empty($coupon_group_id) || empty($user_id) || empty($send_type)) {
            $this->error = "用户领取优惠券出错了：参数不完整！coupon_group_id:{$coupon_group_id},user_id:{$user_id},send_type:{$send_type}";

            return false;
        }
        //获取优惠券主要信息
        $couponGroupInfo = $this->getCouponGroupsInfo($coupon_group_id);
        if ($this->error) {
            $this->error;

            return false;
        }
        //随机获取一张可以使用的优惠券
        $couponInfo = $this->getRandomNoUseCoupon($coupon_group_id, $send_type, $num, $couponGroupInfo);
        if ($this->error) {
            $this->error;

            return false;
        }
        if (empty($couponInfo)) {
            $this->error = "用户领取优惠券出错了：优惠券已经被领取完了！";

            return false;
        }
        //给用户发送优惠券 保存到用户优惠券表
        foreach ($couponInfo as $value) {
            $this->setCouponToUserDb($coupon_group_id, $value['id'], $value['code'], $user_id, $receive_order_no, $props_source);
            if ($this->error) {
                $this->error;

                return false;
            }
        }

        return true;
    }


    /**
     * 获取优惠券信息
     *
     * @param $coupon_group_id
     *
     * @return array|bool
     */
    public function getCouponGroupsInfo($coupon_group_id)
    {
        if (empty($coupon_group_id)) {
            $this->error = "获取优惠券信息出错了：参数不完整！coupon_group_id:{$coupon_group_id}";

            return false;
        }
        $couponGroupsDb   = new IModel('coupon_groups');
        $nowDate          = ITime::getDateTime();
        $where            = "id=$coupon_group_id and status = 1 ";
        $couponGroupsInfo = $couponGroupsDb->getObj($where);
        if (empty($couponGroupsInfo) || $couponGroupsInfo['end_time'] < $nowDate) {
            $this->error = "优惠券已经过期！";

            return false;
        }

        return $couponGroupsInfo;

    }

    /**
     * 随机获取优惠券
     *
     * @param     $coupon_group_id
     * @param     $send_type
     * @param int $num
     * @param     $couponGroupInfo
     *
     * @return array|bool
     */
    public function getRandomNoUseCoupon($coupon_group_id, $send_type, $num = 1, $couponGroupInfo)
    {
        if (empty($coupon_group_id) || empty($send_type)) {
            $this->error = "随机获取一张优惠券码出错了：参数不完整！coupon_group_id:{$coupon_group_id},send_type:{$send_type}";

            return false;
        }

        $couponInfo = array();
        //加入到道具的优惠是没有生成，是要领取道具的在生成写入到优惠券表
        if ($send_type == 2) {
            $couponAddDB = new IModel('coupon');
            for ($i = 0; $i < $num; $i++) {
                $couponData = array(
                    'coupon_group_id' => $coupon_group_id,
                    'send_type'       => $send_type,
                    'create_time'     => ITime::getDateTime(),
                    'status'          => 1,
                    'code'            => $this->getUniqueCode(16),
                    'use_type'        => $couponGroupInfo['use_type']
                );
                $couponAddDB->setData($couponData);
                $couponId = $couponAddDB->add();
                $couponAddDB->commit();
                $couponData['id'] = $couponId;
                $couponInfo[]     = $couponData;
            }
        } else {
            $couponDB        = new IQuery('coupon');
            $couponDB->where = "coupon_group_id=$coupon_group_id and status=1 and send_type=$send_type";
            $couponDB->order = 'rand()';
            $couponDB->limit = $num;
            $couponInfo      = $couponDB->find();
        }

        return $couponInfo;
    }


    /**
     * 给用户发送一张优惠券
     *
     * @param $coupon_group_id
     * @param $coupon_id
     * @param $coupon_code
     * @param $user_id
     * @param $receive_order_no
     * @param $props_source
     *
     * @return bool|int
     */
    public function setCouponToUserDb($coupon_group_id, $coupon_id, $coupon_code, $user_id, $receive_order_no, $props_source)
    {
        if (empty($coupon_group_id) || empty($user_id) || empty($coupon_code)) {
            $this->error = "给用户发送优惠失败：参数不完整！coupon_group_id:{$coupon_group_id},user_id:{$user_id},coupon_code:{$coupon_code}";

            return false;
        }
        $CouponUserDb   = new IModel('coupon_user');
        $CouponUserData = array(
            'coupon_group_id'  => $coupon_group_id,
            'coupon_id'        => $coupon_id,
            'coupon_code'      => $coupon_code,
            'user_id'          => $user_id,
            'redeem_time'      => ITime::getDateTime(),
            'receive_order_no' => $receive_order_no,
            'props_source'     => $props_source,
        );

        $CouponUserDb->setData($CouponUserData);
        $CouponUserId = $CouponUserDb->add();
        $CouponUserDb->commit();
        if ($CouponUserId) {
            $couponDb           = new IModel('coupon');
            $ruleData['status'] = 2;
            $couponDb->setData($ruleData);
            $where = "coupon_group_id=$coupon_group_id and code='" . $coupon_code . "'";
            $couponDb->update($where);
            $couponDb->commit();
        }

        return $CouponUserId;

    }

    /**
     * 生成优惠券uuid
     *
     * @param $length   int 长度
     *
     * @return string   uuid
     */
    public function getUniqueCode($length)
    {
        //前两位数为日期的递增值，用于去重，同时首位必须为字母
        //取得当前天与20150823的差值，并且取26*36作为求余值
        $subDay    = floor(time() / 86400 - 16670);
        $day       = $subDay % 936;
        $dayFirst  = floor($day / 36);
        $daySecond = $day % 36;
        $dayFirst  = chr($dayFirst + 97);
        $daySecond = $daySecond >= 10 ? chr($daySecond + 87) : $daySecond;

        $randomNum = $dayFirst . $daySecond;

        $str = '1234567890abcdefghijklmnopqrstuvwxyz';
        $len = strlen($str) - 1;
        for ($i = 0; $i < $length - 2; $i++) {
            $num       = rand(0, $len);
            $key       = $str[$num];
            $randomNum = $randomNum . $key;
        }

        return $randomNum;
    }


    /**
     * 获取用户的优惠券
     *
     * @param $user_id
     * @param $is_used       0未使用,1已使用
     * @param $coupon_status 0禁用,1可用,2已被领取,3已被使用,4已过期
     *
     * @return array
     */
    public function getUserCoupon($user_id, $is_used = '', $coupon_status = '')
    {
        if (empty($user_id)) {
            return array();
        }

        $couponUserDb = new IModel('coupon_user as cu,coupon as c');
        $where        = "cu.user_id=$user_id and cu.coupon_group_id=c.coupon_group_id and cu.coupon_id=c.id ";
        if ($is_used !== '') {
            if ($is_used == 0) {
                $where .= " and cu.is_used=$is_used and c.status=2";
            } else {
                $where .= " and cu.is_used=$is_used";
            }
        }
        if (!empty($coupon_status)) {
            if (is_array($coupon_status)) {
                $couponStatus = join(",", $coupon_status);
                $where        .= " and c.status in($couponStatus)";
            } else {
                $where .= " and c.status=$coupon_status";
            }
        }
        $filed        = "cu.coupon_group_id,cu.coupon_id,cu.coupon_code,cu.is_used,c.status";
        $order        = "cu.redeem_time desc";
        $limit        = 500;
        $couponResult = $couponUserDb->query($where, $filed, $order, $limit);
        if (empty($couponResult)) {
            return array();
        }
        //获取优惠券主要信息
        $couponGroupArr = array();
        foreach ($couponResult as $value) {
            $couponGroupArr[] = $value['coupon_group_id'];
        }
        $couponGroupStr    = join(',', array_unique($couponGroupArr));
        $couponGroupDb     = new IModel('coupon_groups');
        $couponGroupResult = $couponGroupDb->query("id in(" . $couponGroupStr . ")", "*,date_format(start_time,'%Y-%m-%d') as start_time, date_format(end_time,'%Y-%m-%d') as end_time");
        if (empty($couponGroupResult)) {
            return array();
        }
        $couponGroupInfo = array();
        foreach ($couponGroupResult as $c_value) {
            $couponGroupInfo[$c_value['id']] = $c_value;
        }

        $goodsCouponDb  = new IModel('goods_coupon');
        $goodsCouponRes = $goodsCouponDb->query("coupon_group_id in(" . $couponGroupStr . ")", 'coupon_group_id,goods_id');
        $couponGood     = array();
        if ($goodsCouponRes) {
            foreach ($goodsCouponRes as $g_value) {
                $couponGood[$g_value['coupon_group_id']] .= $g_value['goods_id'] . ",";
            }
        }
        //拼接优惠券信息数据
        $couponInfoData = array();
        foreach ($couponResult as $value) {
            $value['coupon_detail_info'] = $couponGroupInfo[$value['coupon_group_id']];
            $value['goods_list']         = trim($couponGood[$value['coupon_group_id']], ",");
            $couponInfoData []           = $value;
        }

        return $couponInfoData;
    }


    /**
     * 用户使用优惠券
     *
     * @param        $user_id
     * @param        $coupon_code
     * @param        $order_no
     * @param        $seller_id
     *
     * @return bool
     */
    public function useOfCoupon($user_id, $coupon_code, $order_no, $seller_id)
    {
        if (empty($user_id) || empty($coupon_code) || empty($order_no) || empty($seller_id)) {
            $this->error = "用户使用优惠券出错啦：参数不完整！user_id:{$user_id},coupon_code:{$coupon_code},order_no:{$order_no},seller_id:{$seller_id}";

            return false;
        }
        //读取优惠信息 判断是否被使用
        $couponUserAndCouponDb = new IModel('coupon_user as cu,coupon as c');
        $nowDate               = ITime::getDateTime();
        $where                 = "cu.user_id=$user_id and cu.is_used = 0 and cu.coupon_code='$coupon_code' and c.coupon_group_id=cu.coupon_group_id and c.status=2";
        $couponUserInfo        = $couponUserAndCouponDb->getObj($where, 'cu.coupon_group_id,cu.coupon_id,cu.coupon_code,c.status,cu.is_used');
        if (empty($couponUserInfo)) {
            $this->error = "用户使用优惠券出错啦：领取的优惠券异常！";

            return false;
        }
        //读取优惠券主信息 判断优惠是否过期，如果过期 则直接设置过期
        $couponGroupsInfo = $this->getCouponGroupsInfo($couponUserInfo['coupon_group_id']);
        $couponDb         = new IModel('coupon');
        if ($this->error) {
            $this->error;
            $couponDb->setData(array(
                'status'      => 4,
                'update_time' => $nowDate
            ));
            $couponWhere = "code='$coupon_code'";
            $couponDb->update($couponWhere);

            return false;
        }
        //更新用户优惠券表
        $couponUserDb = new IModel('coupon_user');
        $couponUserDb->setData(array(
            'is_used'  => 1,
            'order_no' => $order_no,
            'use_time' => $nowDate,
        ));
        $couponUserWhere = "coupon_code='$coupon_code'";
        $couponUserRes   = $couponUserDb->update($couponUserWhere);
        //更新优惠券表
        $couponDb->setData(array(
            'status'      => 3,
            'update_time' => $nowDate
        ));
        $couponWhere = "code='$coupon_code'";
        $couponRes   = $couponDb->update($couponWhere);

        if ($couponUserRes === false || $couponRes === false) {
            $this->error = "用户使用优惠券出错啦：更新优惠券状态错误！";

            return false;
        }
        $couponPropsDb       = new IModel('coupon_props');
        $couponPropsRes      = $couponPropsDb->getObj("coupon_group_id={$couponUserInfo['coupon_group_id']} and seller_id=$seller_id");
        $userModel           = new IModel('user');
        $userInfo            = $userModel->getObj('id = ' . $user_id);
        $userNameSplitResult = explode('@', $userInfo['username']);
        $userName            = $userNameSplitResult[0];
        $propsParam          = array(
            'user_id'  => $userName,
            'props_id' => $couponPropsRes['props_id'],
            'quantity' => 1,
            'from_id'  => $order_no,
        );
        $userRes             = $this->syncUsePropCoupon($propsParam);
        if ($userRes === false) {
            $this->error = "用户使用优惠券出错啦：同步使用道具出错了！";

            return false;
        }

        return true;
    }

    /**
     * 使用优惠券的时候有加入道具的 要同步核销它
     *
     * @param $propsParam
     *
     * @return bool
     */
    public function syncUsePropCoupon($propsParam)
    {
        if (empty($propsParam['props_id'])) {
            return true;
        }
        $zxlObj = new  zxlhelper();
        $this->logObj->Write('operation', array("优惠券开始使用，同步到.net： " . var_export($propsParam, true)));
        $result = $zxlObj->syncUsePropCoupon($propsParam);

        return $result;
    }

    /**
     * 获取购车里商品是否有可以使用的优惠券
     *
     * @param $good_result
     * @param $user_id
     *
     * @return array|bool
     */
    public function getCartCanUseCoupon($good_result, $user_id)
    {
        if (empty($good_result) || empty($user_id)) {
            return array();
        }
        //组合商品信息
        $goodIds          = array();
        $goodInfo         = array();
        $sellerIdGoodInfo = array();
        foreach ($good_result['goodsList'] as $value) {
            //组合商品id
            $goodIds[] = $value['goods_id'];
            //商品id做下标组合
            $goodInfo[$value['goods_id']] = $value;
            //商品seller_id做下标组合
            $sellerIdGoodInfo[$value['seller_id']][$value['goods_id']] = $value;
        }

        //先获取出用户所有可以使用的优惠券 如果用户没有领取到优惠券直接返回空
        $couponUserDb     = new IModel('coupon_user as cu,coupon as c');
        $couponUseWhere   = "cu.user_id=$user_id and cu.coupon_group_id=c.coupon_group_id and cu.coupon_id=c.id and cu.is_used=0 and c.status=2";
        $filed            = "cu.coupon_group_id,cu.coupon_id,cu.coupon_code,cu.is_used,c.status,c.use_type";
        $order            = "redeem_time desc";
        $couponUserResult = $couponUserDb->query($couponUseWhere, $filed, $order);
        if (empty($couponUserResult)) {
            return array();
        }
        //进行筛选下 用户领取的优惠券哪些是购车里的商品 可以使用的优惠券
        $userCouponAllIds     = array();
        $userCouponAllGroupId = array();
        foreach ($couponUserResult as $cu_value) {
            $userCouponAllIds[]     = $cu_value['coupon_id'];
            $userCouponAllGroupId[] = $cu_value['coupon_group_id'];
        }

        $couponAllIdStr = join(',', $userCouponAllIds);
        //获取下优惠状态已经是否过期正常
        $couponDb          = new IQuery('coupon as c');
        $couponDb->join    = "left join coupon_groups as cg  on cg.id = c.coupon_group_id";
        $couponDb->where   = "c.id in($couponAllIdStr) and c.status = 2 and cg.status=1";
        $couponDb->fields  = "cg.*,c.code,c.id as coupon_id,date_format(cg.start_time,'%Y-%m-%d') as start, date_format(cg.end_time,'%Y-%m-%d') as end";
        $couponGroupsQuery = $couponDb->find();
        //如果优惠券被删除了，没有查询到就返回空
        if (empty($couponGroupsQuery)) {
            return array();
        }
        $userCouponAllGroupIdStr = join(',', $userCouponAllGroupId);
        //取出购车里的商品 哪些是有设置了优惠卷（限制商品的）
        $goodsCouponDb  = new IModel('goods_coupon');
        $goodsCouponRes = $goodsCouponDb->query("coupon_group_id in(" . $userCouponAllGroupIdStr . ")", 'coupon_group_id,goods_id');
        //组合购买的商品里设置了哪些优惠可以使用 组合设置优惠券后的商品信息
        $couponGoodsInfo = array();
        if ($goodsCouponRes) {
            foreach ($goodsCouponRes as $value) {
                $couponGoodsInfo[$value['coupon_group_id']][] = $value['goods_id'];
            }
        }

        $couponGroupGoodsInfo = array();
        //组合优惠卷和商品有关联的
        foreach ($couponGroupsQuery as $value) {
            $value['goods_list']    = $couponGoodsInfo[$value['id']];
            $couponGroupGoodsInfo[] = $value;
        }
        $endCanUseCoupon    = array();
        $endCanNotUseCoupon = array();
        $nowDate            = ITime::getTime();
        foreach ($couponGroupGoodsInfo as $coupon_val) {
            if (strtotime($coupon_val['start_time']) > $nowDate) {
                $coupon_val['unable_use_reason'] = "未到使用日期";
                array_push($endCanNotUseCoupon, $coupon_val);
                continue;
            }
            //判断是否结束过期了
            if (strtotime($coupon_val['end_time']) < $nowDate) {
                $coupon_val['unable_use_reason'] = "优惠券已经过期";
                array_push($endCanNotUseCoupon, $coupon_val);
                continue;
            }
            //判断购物车的商品 是否符合的优惠卷
            if (!array_key_exists($coupon_val['seller_id'], $sellerIdGoodInfo)) {
                $coupon_val['unable_use_reason'] = "所结算商品中没有符合条件的商品";
                array_push($endCanNotUseCoupon, $coupon_val);
                continue;
            }
            $full_money = $coupon_val['full_money'];
            $full_point = $coupon_val['full_point'];
            //根据租户拆分 计算全部的支付金额和积分
            $sum_pay_price_sum = '';
            $sum_pay_point_sum = '';

            foreach ($sellerIdGoodInfo[$coupon_val['seller_id']] as $good_val) {
                $sum_pay_price_sum += $good_val['pay_price_sum'];
                $sum_pay_point_sum += $good_val['pay_point_sum'];
            }
            //全部商品 但是要满足  满多少减的
            if ($coupon_val['use_type'] == 1 && $coupon_val['condition'] == 2) {
                if ($full_money && $sum_pay_price_sum < $full_money) {
                    $coupon_val['unable_use_reason'] = "未满足使用条件";
                    array_push($endCanNotUseCoupon, $coupon_val);
                    continue;
                }
                if ($full_point && $sum_pay_point_sum < $full_point) {
                    $coupon_val['unable_use_reason'] = "未满足使用条件";
                    array_push($endCanNotUseCoupon, $coupon_val);
                    continue;
                }
            }
            $isCanNot = false;

            //限制部分商品 不用满足金额的
            if ($coupon_val['use_type'] == 2 && $coupon_val['condition'] == 1) {
                $goodListStr = join(",", $coupon_val['goods_list']);
                //根据租户拆分 计算 满足优惠卷限商品的 的支付金额和积分
                foreach ($sellerIdGoodInfo[$coupon_val['seller_id']] as $good_id_key => $seller_good_val) {
                    if (stripos($goodListStr, (string)$good_id_key) !== false) {
                        $isCanNot = false;
                        break;
                    } else {
                        $isCanNot = true;
                    }
                }
                if ($isCanNot == true) {
                    $coupon_val['unable_use_reason'] = "所结算商品中没有符合条件的商品";
                    array_push($endCanNotUseCoupon, $coupon_val);
                    continue;
                }
            }

            //限制部分商品
            if ($coupon_val['use_type'] == 2 && $coupon_val['condition'] == 2) {
                //根据租户拆分 计算 满足优惠卷限商品的 的支付金额和积分
                $part_pay_price_sum = '';
                $part_pay_point_sum = '';
                //部分商品 金额和积分合
                foreach ($sellerIdGoodInfo[$coupon_val['seller_id']] as $good_val) {
                    if (in_array($good_val['goods_id'], $coupon_val['goods_list'])) {
                        $part_pay_price_sum += $good_val['pay_price_sum'];
                        $part_pay_point_sum += $good_val['pay_point_sum'];
                    }
                }

                if ($full_money && $part_pay_price_sum < $full_money) {
                    $coupon_val['unable_use_reason'] = "未满足使用条件";
                    array_push($endCanNotUseCoupon, $coupon_val);
                    continue;
                }
                if ($full_point && $part_pay_point_sum < $full_point) {
                    $coupon_val['unable_use_reason'] = "未满足使用条件";
                    array_push($endCanNotUseCoupon, $coupon_val);
                    continue;
                }
            }
            $endCanUseCoupon[] = $coupon_val;
        }
        $data['can_use_coupon']     = $endCanUseCoupon;
        $data['can_not_use_coupon'] = $endCanNotUseCoupon;

        return $data;
    }

    /**
     * 优惠使用校验以及使用优惠券后金额积分结算
     *
     * @param $buyGoodsList
     * @param $couponCodeArr
     *
     * @return array|bool
     */
    public function useCouponCheckAndBalance($buyGoodsList, $couponCodeArr)
    {
        if (empty($buyGoodsList)) {
            $this->error['code'] = "error";
            $this->error['msg']  = "没有购买商品信息";

            return false;
        }
        if (empty($couponCodeArr)) {
            $this->error['code'] = "no_use";
            $this->error['msg']  = "没有使用优惠券";

            return $buyGoodsList;
        }
        //组合商品信息
        $goodIds          = array();
        $goodInfo         = array();
        $sellerIdGoodInfo = array();
        foreach ($buyGoodsList['goodsList'] as $value) {
            //组合商品id
            $goodIds[] = $value['goods_id'];
            //商品id做下标组合
            $goodInfo[$value['goods_id']] = $value;
            //商品seller_id做下标组合
            $sellerIdGoodInfo[$value['seller_id']][$value['goods_id']] = $value;
        }
        $couponCodeStr = join("','", $couponCodeArr);
        //获取下优惠状态已经是否过期正常
        $couponDb         = new IQuery('coupon as c');
        $couponDb->join   = "left join coupon_groups as cg  on cg.id = c.coupon_group_id  ";
        $couponDb->where  = "c.code in('$couponCodeStr') and c.status = 2 and cg.status=1";
        $couponDb->fields = "cg.*,c.code,c.id as coupon_id,c.coupon_group_id";
        $couponQuery      = $couponDb->find();
        if (empty($couponQuery)) {
            $this->error['code'] = "error";
            $this->error['msg']  = "优惠券异常，没有查询到您的优惠券";

            return false;
        }

        //判断优惠券是否开始了 是否过期 一个商品只能使用限定的一张优惠券
        $nowDate                 = ITime::getTime();
        $canUseCoupon            = array();
        $allCanUseCouponGroupIds = array();
        foreach ($couponQuery as $value) {
            if (strtotime($value['start_time']) > $nowDate) {
                $this->error['code'] = "error";
                $this->error['msg']  = "您使用的优惠券:" . $value['code'] . "异常了，还没到开始时间";

                return false;
            }
            //判断是否结束过期了
            if (strtotime($value['end_time']) < $nowDate) {
                $this->error['code'] = "error";
                $this->error['msg']  = "您使用的优惠券:" . $value['code'] . "异常了，已经过期了";

                return false;
            }
            if (array_key_exists($value['seller_id'], $canUseCoupon)) {
                $this->error['code'] = "error";
                $this->error['msg']  = "同类型的券该订单只可使用一张";

                return false;
            } else {
                $canUseCoupon[$value['seller_id']] = $value;
                $allCanUseCouponGroupIds[]         = $value['coupon_group_id'];
            }
        }

        //取出购车里的商品 哪些是有设置了优惠卷（限制商品的）
        $couponGroupIdStr = join(',', $allCanUseCouponGroupIds);
        $goodsCouponDb    = new IModel('goods_coupon');
        $goodsCouponRes   = $goodsCouponDb->query("coupon_group_id in(" . $couponGroupIdStr . ")", 'coupon_group_id,goods_id');
        //组合购买的商品里设置了哪些优惠可以使用 组合设置优惠券后的商品信息
        $couponGoodsInfo = array();
        if ($goodsCouponRes) {
            foreach ($goodsCouponRes as $good_value) {
                $couponGoodsInfo[$good_value['coupon_group_id']][] = $good_value['goods_id'];
            }
        }
        if ($couponGoodsInfo) {
            foreach ($canUseCoupon as &$value) {
                $value['goods_list'] = $couponGoodsInfo[$value['coupon_group_id']];
            }
        }
        $coupon_share_goods = array();
        $discount           = array();
        //校验优惠是否满足使用条件
        foreach ($canUseCoupon as $coupon_value) {

            $full_money = $coupon_value['full_money'];
            $full_point = $coupon_value['full_point'];
            //根据租户拆分 计算全部的支付金额和积分
            $sum_pay_price_sum = '';
            $sum_pay_point_sum = '';
            foreach ($sellerIdGoodInfo[$coupon_value['seller_id']] as $good_val) {
                $sum_pay_price_sum += $good_val['pay_price_sum'];
                $sum_pay_point_sum += $good_val['pay_point_sum'];
            }
            //全部商品 但是要满足  满多少减的
            if ($coupon_value['use_type'] == 1 && $coupon_value['condition'] == 2) {
                if ($full_money && $sum_pay_price_sum < $full_money) {
                    $this->error['code'] = "error";
                    $this->error['msg']  = "您使用的优惠券:" . $coupon_value['code'] . " 未满足使用条件：" . " 满{$full_money}元可用";

                    return false;
                }
                if ($full_point && $sum_pay_point_sum < $full_point) {
                    $this->error['code'] = "error";
                    $this->error['msg']  = "您使用的优惠券:" . $coupon_value['code'] . " 未满足使用条件：" . " 满{$full_point}积分可用";

                    return false;
                }
            }

            //限制部分商品
            if ($coupon_value['use_type'] == 2 && $coupon_value['condition'] == 2) {
                //根据租户拆分 计算 满足优惠卷限商品的 的支付金额和积分
                $part_pay_price_sum = '';
                $part_pay_point_sum = '';
                //部分商品 金额和积分合
                foreach ($sellerIdGoodInfo[$coupon_value['seller_id']] as $good_val) {
                    if (in_array($good_val['goods_id'], $coupon_value['goods_list'])) {
                        $part_pay_price_sum += $good_val['pay_price_sum'];
                        $part_pay_point_sum += $good_val['pay_point_sum'];
                    }
                }
                if ($full_money && $part_pay_price_sum < $full_money) {
                    $this->error['code'] = "error";
                    $this->error['msg']  = "您使用的优惠券:" . $coupon_value['code'] . " 未满足使用条件：" . " 满{$full_money}元可用";

                    return false;
                }
                if ($full_point && $part_pay_point_sum < $full_point) {
                    $this->error['code'] = "error";
                    $this->error['msg']  = "您使用的优惠券:" . $coupon_value['code'] . " 未满足使用条件：" . " 满{$full_point}积分可用";

                    return false;
                }
            }
            //计算优惠卷 优惠卷总的价格
            $discount['discount_point_sum']                          += $coupon_value['discount_point'];
            $discount['discount_price_sum']                          += $coupon_value['discount_money'];
            $discount['use_coupon_code']                             .= $coupon_value['code'] . ",";
            $discount['use_coupon_info'][$coupon_value['seller_id']] = array(
                'code'           => $coupon_value['code'],
                'discount_point' => $coupon_value['discount_point'],
                'discount_money' => $coupon_value['discount_money']
            );
            //使用优惠券 优惠的额度根据商品进行平摊
            if ($coupon_value['use_type'] == 1) {
                foreach ($sellerIdGoodInfo[$coupon_value['seller_id']] as &$coupon_good_val) {
                    if ($sum_pay_price_sum == 0) {
                        $sum_pay_price_sum = 1;
                    }
                    if ($sum_pay_point_sum == 0) {
                        $sum_pay_point_sum = 1;
                    }
                    $discount_price                    = sprintf("%.2f", ($coupon_good_val['pay_price_sum'] / $sum_pay_price_sum) * $coupon_value['discount_money']);
                    $discount_point                    = sprintf("%.2f", ($coupon_good_val['pay_point_sum'] / $sum_pay_point_sum) * $coupon_value['discount_point']);
                    $coupon_good_val['discount_price'] = $discount_price;
                    $coupon_good_val['discount_point'] = $discount_point;
                }
            }
            if ($coupon_value['use_type'] == 2) {
                $part_pay_price_sum = '';
                $part_pay_point_sum = '';
                //部分商品 金额和积分合
                foreach ($sellerIdGoodInfo[$coupon_value['seller_id']] as $good_val) {
                    if (in_array($good_val['goods_id'], $coupon_value['goods_list'])) {
                        $part_pay_price_sum += $good_val['pay_price_sum'];
                        $part_pay_point_sum += $good_val['pay_point_sum'];
                    }
                }
                foreach ($sellerIdGoodInfo[$coupon_value['seller_id']] as &$coupon_good_val) {
                    if (in_array($coupon_good_val['goods_id'], $coupon_value['goods_list'])) {
                        if ($part_pay_price_sum == 0) {
                            $part_pay_price_sum = 1;
                        }
                        if ($part_pay_point_sum == 0) {
                            $part_pay_point_sum = 1;
                        }
                        $discount_price                    = sprintf("%.2f", ($coupon_good_val['pay_price_sum'] / $part_pay_price_sum) * $coupon_value['discount_money']);
                        $discount_point                    = sprintf("%.2f", ($coupon_good_val['pay_point_sum'] / $part_pay_point_sum) * $coupon_value['discount_point']);
                        $coupon_good_val['discount_price'] = $discount_price;
                        $coupon_good_val['discount_point'] = $discount_point;
                    }
                }
            }
            $coupon_share_goods[$coupon_value['seller_id']] = $sellerIdGoodInfo[$coupon_value['seller_id']];

        }
        //数据整合会提交进来 商品列表
        foreach ($buyGoodsList['goodsList'] as &$buy_good_value) {
            $buy_good_value['discount_price'] = $coupon_share_goods[$buy_good_value['seller_id']][$buy_good_value['goods_id']]['discount_price'];
            $buy_good_value['discount_point'] = $coupon_share_goods[$buy_good_value['seller_id']][$buy_good_value['goods_id']]['discount_point'];
        }
        $goodsList = array();
        foreach ($buyGoodsList['goodsList'] as $goods_list_value) {
            $goodsList[$goods_list_value['goods_id']] = $goods_list_value;
        }
        $buyGoodsList['goodsList']    = $goodsList;
        $discount['use_coupon_count'] = count($canUseCoupon);
        $data                         = array_merge($buyGoodsList, $discount);

        return $data;
    }

    /**
     *
     * 获取优惠券当成商品售卖的 订单详情里的 优惠券
     *
     * @param $couponGroupIds
     * @param $user_id
     * @param $receive_order_no
     *
     * @return array
     */
    public function getOrderDetailCoupon($couponGroupIds, $user_id, $receive_order_no)
    {
        if (empty($couponGroupIds) || empty($user_id)) {
            return array();
        }

        $couponGroupIdStr = join("','", $couponGroupIds);
        //获取下优惠状态已经是否过期正常
        $couponDb         = new IQuery('coupon_user as cu');
        $couponDb->join   = "left join coupon as c  on c.id = cu.coupon_id";
        $couponDb->where  = "cu.coupon_group_id in($couponGroupIdStr) and cu.user_id=$user_id and receive_order_no=$receive_order_no";
        $couponDb->fields = "cu.*,c.*";
        $couponQuery      = $couponDb->find();
        if (empty($couponQuery)) {
            return array();
        }
        $couponData = array();
        foreach ($couponQuery as $key => $value) {
            $couponData[$value['coupon_group_id']][] = $value;
        }

        return $couponData;
    }

    /**
     * 更新订单里相关联的优惠卷
     *
     * @param $couponGroupIds
     * @param $user_id
     * @param $receive_order_no
     * @param $set_status
     * @param $where_status
     *
     * @return bool
     */
    public function updateOrderDetailCoupon($couponGroupIds, $user_id, $receive_order_no, $set_status, $where_status)
    {
        if (empty($couponGroupIds) || empty($user_id) || empty($set_status) || empty($where_status)) {
            return false;
        }

        $db                  = IDBFactory::getDB();
        $couponTableName     = IWeb::$app->config['DB']['tablePre'] . "coupon";
        $couponUserTableName = IWeb::$app->config['DB']['tablePre'] . "coupon_user";
        $sql                 = "UPDATE {$couponTableName} as c  LEFT JOIN  {$couponUserTableName} as cu ON cu.coupon_id= c.id 
                                set  c.status = {$set_status} 
                                where  cu.coupon_group_id={$couponGroupIds} and cu.user_id={$user_id} and c.status={$where_status} and cu.receive_order_no={$receive_order_no}";
        $upRes               = $db->query($sql);

        return $upRes;
    }

    /**
     * 订单过期未支付 优惠卷退回
     *
     * @param $order_no
     * @param $from_seller_id
     *
     * @return bool|int
     */
    public static function unPayRestore($order_no, $from_seller_id)
    {
        if (empty($order_no)) {
            return false;
        }
        //更新用户优惠券表
        $couponUserDb     = new IModel('coupon_user');
        $couponUserResult = $couponUserDb->query("order_no=" . $order_no);
        if (empty($couponUserResult)) {
            return true;
        }
        $couponDb = new IModel('coupon');
        $logObj   = new Log();
        foreach ($couponUserResult as $value) {
            $couponUserDb->setData(array(
                'is_used' => 0,
                'remark'  => " 订单超时未支付，优惠券退回！"
            ));
            $couponUserWhere = "coupon_id={$value['coupon_id']}";
            $couponUserUpRes = $couponUserDb->update($couponUserWhere);
            if ($couponUserUpRes) {
                $couponDb->setData(array(
                    'update_time' => ITime::getTime(),
                    'status'      => 2
                ));
                $couponWhere = "id={$value['coupon_id']}";
                $couponDb->update($couponWhere);
            }
            $userModel           = new IModel('user');
            $userInfo            = $userModel->getObj('id = ' . $value['user_id']);
            $userNameSplitResult = explode('@', $userInfo['username']);
            $userName            = $userNameSplitResult[0];
            $tenantId            = $userNameSplitResult[1];
            $tenantTokenResult   = SystemToken::getTenantTokenList(array($tenantId));
            if ($tenantTokenResult['failure']) {
                $logObj->Write('error', array('订单超时未支付，优惠券退回，同步到道具获取token失败！：' . explode(',', $tenantTokenResult['failure']) . "优惠券:{$value['coupon_code']}，用户信息user_id:{$value['user_id']}"));
                continue;
            }
            $token            = rawurldecode($tenantTokenResult['success'][$tenantId]);
            $couponPropsModel = new IModel('coupon_props');
            $propsInfo        = $couponPropsModel->getObj('coupon_group_id = ' . $value['coupon_group_id'] . ' and seller_id =' . $from_seller_id);
            $propsParam       = array(
                'user_id'  => $userName,
                'props_id' => $propsInfo['props_id'],
                'quantity' => 1,
                'from_id'  => $order_no,
                'token'    => $token,
            );
            $zxlObj           = new  zxlhelper();
            $returnResult     = $zxlObj->returnUserCouponProps($propsParam);
            if ($returnResult === false) {
                $logObj->Write('error', array("优惠券退回，回调.net失败"));
            }

        }

        return true;
    }

}